package rexsee.core.widget;

import rexsee.core.menu.PopMenu;
import rexsee.core.menu.PopMenuItem;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class ButtonBar extends Div {

	public final LinearLayout mContainer;
	private final HScrollView mScrollerX;
	private final ScrollView mScrollerY;

	public ButtonBar(final Context context, PopMenu popMenu) {
		this(context, popMenu, "");
	}
	public ButtonBar(Context context, PopMenu popMenu, String style) {
		super(context);

		setDivId(popMenu.id);
		styleSheet = popMenu.styleSheet.clone().parseStyle(style);

		mContainer = new LinearLayout(context);
		mContainer.setGravity(styleSheet.getGravity());
		if (styleSheet.orientation.equalsIgnoreCase("x") || styleSheet.orientation.equalsIgnoreCase("horizontal")) {
			mContainer.setOrientation(LinearLayout.HORIZONTAL);
		} else {
			mContainer.setOrientation(LinearLayout.VERTICAL);
		}
		for (int i = 0; i < popMenu.items.size(); i++) {
			final PopMenuItem item = popMenu.items.get(i);
			Button button = new Button(context);
			button.setText(Html.fromHtml(item.styleNormal.label));
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (item.upRunnable != null) item.upRunnable.run();
				}
			});
			button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 1));
			mContainer.addView(button);
		}

		mScrollerX = new HScrollView(context) {
			@Override
			public int getSolidColor() {
				try {
					return Color.parseColor(styleSheet.fading_edge_x_color);
				} catch (Exception e) {
					return Color.BLACK;
				}
			}
		};
		mScrollerY = new ScrollView(context) {
			@Override
			public int getSolidColor() {
				try {
					return Color.parseColor(styleSheet.fading_edge_y_color);
				} catch (Exception e) {
					return Color.BLACK;
				}
			}
		};

		try {

			if (styleSheet.overflow_x.equalsIgnoreCase("visible") && styleSheet.overflow_y.equalsIgnoreCase("visible")) {
				mScrollerX.addView(mContainer);
				mScrollerY.addView(mScrollerX);
				this.addDivChild(mScrollerY);
			} else if (styleSheet.overflow_x.equalsIgnoreCase("visible")) {
				mScrollerX.addView(mContainer);
				this.addDivChild(mScrollerX);
			} else if (styleSheet.overflow_y.equalsIgnoreCase("visible")) {
				mScrollerY.addView(mContainer);
				this.addDivChild(mScrollerY);
			} else {
				this.addDivChild(mContainer);
			}

			setBarStyle();

		} catch (Exception e) {
			Toast.makeText(context, "TabBar:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}

	}

	public void setBarStyle(String style) {
		styleSheet.parseStyle(style);
		setBarStyle();
	}
	public void setBarStyle() {
		try {
			if (styleSheet.overflow_x.equalsIgnoreCase("visible")) {
				mScrollerX.setHorizontalFadingEdgeEnabled(true);
				mScrollerX.setFadingEdgeLength(Integer.parseInt(styleSheet.fading_edge_x_length.toLowerCase().replaceAll("px", "")));
				if (styleSheet.scrollbar_x.equalsIgnoreCase("hidden")) {
					mScrollerX.setHorizontalScrollBarEnabled(false);
				} else {
					mScrollerX.setHorizontalScrollBarEnabled(true);
					mScrollerX.setScrollBarStyle(styleSheet.getScrollbarXStyle());
					mScrollerX.setScrollbarFadingEnabled((styleSheet.scrollbar_x.equalsIgnoreCase("visible")) ? false : true);
				}
			}
			if (styleSheet.overflow_y.equalsIgnoreCase("visible")) {
				mScrollerY.setVerticalFadingEdgeEnabled(true);
				mScrollerY.setFadingEdgeLength(Integer.parseInt(styleSheet.fading_edge_y_length.toLowerCase().replaceAll("px", "")));
				if (styleSheet.scrollbar_y.equalsIgnoreCase("hidden")) {
					mScrollerY.setVerticalScrollBarEnabled(false);
				} else {
					mScrollerY.setVerticalScrollBarEnabled(true);
					mScrollerY.setScrollBarStyle(styleSheet.getScrollbarYStyle());
					mScrollerY.setScrollbarFadingEnabled((styleSheet.scrollbar_y.equalsIgnoreCase("visible")) ? false : true);
				}
			}
		} catch (Exception e) {
			Toast.makeText(mContext, "TabBar:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
		setDivStyle();
	}

}
